package com.shirotest.shiro;

import java.io.Serializable;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import com.shirotest.utility.StringUtility;

public class MySessionManager extends DefaultWebSessionManager {
	/**
	 * 接到前端请求时，需要提供获取sessionId的方法，用于根据sessionId获取对应的会话数据
	 */
	@Override
	protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
		String sessionId = request.getParameter("sessionId");
		if(!StringUtility.isEmpty(sessionId)) {
			request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, ShiroHttpServletRequest.COOKIE_SESSION_ID_SOURCE);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, sessionId);
            request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
			return sessionId;
		}
		return super.getSessionId(request, response);
	}
}
